import deviceApi from "../../api/deviceInfo";
import { toast } from "../../utils/util";
Page({
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    switchVal: true,
    tabs: [
      { value: 'label_1', icon: 'icon-a-Frame1620', label: '开机' },
      { value: 'label_2', icon: 'icon-a-Frame1621', label: '关机' },
      { value: 'label_3', icon: 'icon-shanchu', label: '删除' },
    ],
    visible: false,
    popupStyle: 'padding-bottom: 0',
    pageSize: 10,
    pageNum: 1,
    allList: [],
    deviceNum: 0,
    deviceId: []
  },
  onLoad(options) {
    this.init();
  },
  init () {
    let params = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
    }
    deviceApi.getUserDeviceList(params).then(res => {
      this.data.allList = this.data.allList.concat(res.rows);
      let arr = this.data.allList.filter(item => {
        if(item.deviceInfoVo.onLine > 0) {
          return item.deviceId
        }
      })
      this.data.pageNum++;
      this.setData({
        allList: this.data.allList,
        pageNum: this.data.pageNum,
        deviceId: arr
      });
      if(res.rows.length < this.data.pageSize){
        this.setData({
          status: 'noMore',
        })
      }
    });
  },
  handleChange(e) {
    let arr = this.data.deviceId;
    let allList = this.data.allList;
    let { index, id } = e.currentTarget.dataset;
    let status = allList[index].deviceInfoVo.onLine ? true : false;
    allList[index].deviceInfoVo.onLine = !status;
    allList[index].deviceInfoVo.onLine = allList[index].deviceInfoVo.onLine ? 1 : 0;
    if (!status) {
      arr.push(id);
    } else {
      const ind = arr.findIndex(item => item === id);
      arr.splice(ind,1);
    }
    this.setData({
      allList: allList,
      deviceNum: arr.length,
      deviceId: arr
    });
  },
  editDevice (e) {
    if(!this.data.deviceId.length) {
      return toast('请选择设备')
    }
    let { value } = e.currentTarget.dataset
    this.setData({
      visible: true,
    })
    if(value === 'label_1') {
      this.setData({
        title: '开机',
        content: '确认对选择的设备全部开机'
      })
    }
    if(value === 'label_2') {
      this.setData({
        title: '关机',
        content: '确认对选择的设备全部关机'
      })
    }
    if(value === 'label_3') {
      this.setData({
        title: '删除',
        content: '确认对选择的设备全部删除'
      })
    }
    
  },
  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },
  chooseAll() {
    let list = this.data.allList;
    list.forEach(item => {
      item.deviceInfoVo.onLine = 1
    })
    let arr = list.map(item => {
      return item.id
    })
    this.setData({
      allList: list,
      deviceNum: list.length,
      deviceId: arr
    })
  },
  cancel () {
    let list = this.data.allList;
    list.forEach(item => {
      item.deviceInfoVo.onLine = 0
    })
    this.setData({
      allList: list,
      deviceNum: 0,
      deviceId: []
    })
  },
  cancelVisible () {
    this.setData({
      visible: false
    })
  },
  onSubmit () {
    this.setData({
      visible: false
    })
    return toast('功能暂不支持');
  }
})
